//
// Created by HP on 2024/1/1.
//

#ifndef AURORA_ST_EFLASH_DRIVER_H
#define AURORA_ST_EFLASH_DRIVER_H

//外部flash 驱动
#include "common_inc.h"


#define EFLASH_CS_H    HAL_GPIO_WritePin(FLASH_CS_GPIO_Port,FLASH_CS_Pin,GPIO_PIN_SET)
#define EFLASH_CS_L    HAL_GPIO_WritePin(FLASH_CS_GPIO_Port,FLASH_CS_Pin,GPIO_PIN_RESET)


class Eflash_Driver{

public:
#define MAX_ADDR        0x1000000    //16M
#define BLOCK_SIZE      0x10000     //128个块
#define SECTOR_SIZE     0x1000      //4096
#define FLASH_PAGE_SIZE 0x100       //256

    Eflash_Driver();

    uint16_t read_jedec_id();
    void read_data(uint32_t addr,uint8_t *rbuff,uint32_t len);
    void write_data(uint32_t addr,uint8_t *wbuff,uint32_t len);

    void write_eeprom_byte_data(uint32_t addr,uint8_t data);
    void write_eeprom_halfword_data(uint32_t addr,uint16_t data);
    void write_eeprom_word_data(uint32_t addr,uint32_t data);


    void chip_erase();
    void sector_erase(uint32_t addr);
    void block_erase(uint32_t addr);

    uint32_t get_block_size();
    uint32_t get_sector_size();

private:
    void _set_spi_mode();

    void _eflash_read_info(uint8_t inst,uint8_t *rbuff,uint16_t len);
    void _elfash_wenable();
    void _elfash_busy_wait();
private:
    SPI_HandleTypeDef *hspi;



};


#endif //AURORA_ST_EFLASH_DRIVER_H
